package com.fept.msgcore.msgserver.util.json;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONBuilder;
import net.sf.json.util.JSONStringer;

/**
 * Created by Srma on 2/9/2017.
 */
public class JSONUtil {
    public JSONUtil() {
    }

//    public static <T> T jsonToBean(String jsonString, Class<T> beanCalss) {
//        JSONObject jsonObject = JSONObject.fromObject(jsonString);
//        Object bean = JSONObject.toBean(jsonObject, beanCalss);
//        return bean;
//    }

    public static String beanArrayToJson(Object[] beans) {
        if(beans == null) {
            return "[]";
        } else {
            ArrayList objList = new ArrayList();
            Object[] var5 = beans;
            int var4 = beans.length;

            for(int var3 = 0; var3 < var4; ++var3) {
                Object obj = var5[var3];
                objList.add(obj);
            }

            return beanListToJson(objList);
        }
    }

    public static String beanToJson(Object bean) {
        JSONObject json = JSONObject.fromObject(bean);
        return json.toString();
    }

    public static String beanToJson(Object bean, String[] _nory_changes, boolean nory) {
        JSONObject json = null;
        if(nory) {
            Field[] fields = bean.getClass().getDeclaredFields();
            String str = "";
            Field[] var9 = fields;
            int var8 = fields.length;

            Field s;
            int var7;
            for(var7 = 0; var7 < var8; ++var7) {
                s = var9[var7];
                str = str + ":" + s.getName();
            }

            fields = bean.getClass().getSuperclass().getDeclaredFields();
            var9 = fields;
            var8 = fields.length;

            for(var7 = 0; var7 < var8; ++var7) {
                s = var9[var7];
                str = str + ":" + s.getName();
            }

            str = str + ":";
            String[] var11 = _nory_changes;
            var8 = _nory_changes.length;

            for(var7 = 0; var7 < var8; ++var7) {
                String var10 = var11[var7];
                str = str.replace(":" + var10 + ":", ":");
            }

            json = JSONObject.fromObject(bean, configJson(str.split(":")));
        } else {
            json = JSONObject.fromObject(bean, configJson(_nory_changes));
        }

        return json.toString();
    }

    private static JsonConfig configJson(String[] excludes) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        jsonConfig.setIgnoreDefaultExcludes(false);
        return jsonConfig;
    }

    public static String beanListToJson(List beans) {
        StringBuffer rest = new StringBuffer();
        rest.append("[");
        int size = beans.size();

        for(int i = 0; i < size; ++i) {
            rest.append(beanToJson(beans.get(i)) + (i < size - 1?",":""));
        }

        rest.append("]");
        return rest.toString();
    }

    public static String beanListToJson(List beans, String[] _nory_changes, boolean nory) {
        StringBuffer rest = new StringBuffer();
        rest.append("[");
        int size = beans.size();

        for(int i = 0; i < size; ++i) {
            try {
                rest.append(beanToJson(beans.get(i), _nory_changes, nory));
                if(i < size - 1) {
                    rest.append(",");
                }
            } catch (Exception var7) {
                var7.printStackTrace();
            }
        }

        rest.append("]");
        return rest.toString();
    }

    public static Map jsonToMap(String jsonString) {
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        Iterator keyIter = jsonObject.keys();
        HashMap valueMap = new HashMap();

        while(keyIter.hasNext()) {
            String key = (String)keyIter.next();
            String value = jsonObject.get(key).toString();
            valueMap.put(key, value);
        }

        return valueMap;
    }

    public static String mapToJson(Map<String, ?> map, String[] _nory_changes, boolean nory) {
        String s_json = "{";
        Set key = map.keySet();
        Iterator it = key.iterator();

        while(it.hasNext()) {
            String s = (String)it.next();
            if(map.get(s) != null) {
                if(map.get(s) instanceof List) {
                    s_json = s_json + s + ":" + beanListToJson((List)map.get(s), _nory_changes, nory);
                } else {
                    JSONObject json = JSONObject.fromObject(map);
                    s_json = s_json + s + ":" + json.toString();
                }
            }

            if(it.hasNext()) {
                s_json = s_json + ",";
            }
        }

        s_json = s_json + "}";
        return s_json;
    }

    public static Object[] jsonToObjectArray(String jsonString) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        return jsonArray.toArray();
    }

    public static String listToJson(List<?> list) {
        JSONArray jsonArray = JSONArray.fromObject(list);
        return jsonArray.toString();
    }

    public static <T> List<T> jsonToBeanList(String jsonString, Class<T> beanClass) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        int size = jsonArray.size();
        ArrayList list = new ArrayList(size);

        for(int i = 0; i < size; ++i) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Object bean = JSONObject.toBean(jsonObject, beanClass);
            list.add(bean);
        }

        return list;
    }

    public static String stringListToJson(List<String> stringList) {
        JSONStringer jsonStringer = new JSONStringer();
        JSONBuilder jb = jsonStringer.array();
        Iterator var4 = stringList.iterator();

        while(var4.hasNext()) {
            String str = (String)var4.next();
            jb.value(str);
        }

        jb.endArray();
        return jb.toString();
    }

    public static List<String> jsonToStringList(String jsonString) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        ArrayList stringList = new ArrayList();
        int size = jsonArray.size();

        for(int i = 0; i < size; ++i) {
            stringList.add(jsonArray.getString(i));
        }

        return stringList;
    }

    public static String[] jsonToStringArray(String jsonString) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        String[] stringArray = new String[jsonArray.size()];
        int size = jsonArray.size();

        for(int i = 0; i < size; ++i) {
            stringArray[i] = jsonArray.getString(i);
        }

        return stringArray;
    }

    public static Long[] jsonToLongArray(String jsonString) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        int size = jsonArray.size();
        Long[] longArray = new Long[size];

        for(int i = 0; i < size; ++i) {
            longArray[i] = Long.valueOf(jsonArray.getLong(i));
        }

        return longArray;
    }

    public static Integer[] jsonToIntegerArray(String jsonString) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        int size = jsonArray.size();
        Integer[] integerArray = new Integer[size];

        for(int i = 0; i < size; ++i) {
            integerArray[i] = Integer.valueOf(jsonArray.getInt(i));
        }

        return integerArray;
    }

    public static Double[] jsonToDoubleArray(String jsonString) {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        int size = jsonArray.size();
        Double[] doubleArray = new Double[size];

        for(int i = 0; i < size; ++i) {
            doubleArray[i] = Double.valueOf(jsonArray.getDouble(i));
        }

        return doubleArray;
    }
}
